package com.hcbd.tdwt.ui.xingyu.fragment;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.base.Listener.InfoInterface;
import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.util.SharedPreferencesUtil;
import com.hcbd.tdwt.util.StringUtil;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGGAInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDRMCInfo;
import com.hcbd.tdwt.ui.xingyu.vo.SatelliteVO;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.List;


@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_position_info)
public class PositionInfoFragment extends BaseFragment implements InfoInterface {
    @ViewById
    TextView longitudeTv,latitudeTv,altitudeTv,positionStatus;
    @ViewById
    TextView speedTv,directionTv,satelliteNumber,hdopTv;
    @ViewById
    View dotted_view_1,dotted_view_2,dotted_view_3;
    @ViewById
    View dotted_view_vertical_1,dotted_view_vertical_2,dotted_view_vertical_3,dotted_view_vertical_4;
    @ViewById
    LinearLayout positionCenterLL;

    @ViewById
    LinearLayout bgLL;
    SharedPreferencesUtil sp;
    private  int format=0;
    @AfterViews
    protected void init() {
        sp=new SharedPreferencesUtil(this.getContext());
        format=sp.getAttributeInt(Constant.LATITUDE_AND_LONGITUDE_FORMAT);
        if(BdToolFactory.app_type==BdToolFactory.FU_DA_BG_TYPE||BdToolFactory.app_type==BdToolFactory.HLCT_BG_TYPE){
            bgLL.setBackground(getContext().getDrawable(R.color.white));
            dotted_view_1.setBackground(getContext().getDrawable(R.drawable.shape_dash_blue_100_line));
            dotted_view_2.setBackground(getContext().getDrawable(R.drawable.shape_dash_blue_100_line));
            dotted_view_3.setBackground(getContext().getDrawable(R.drawable.shape_dash_blue_100_line));
            dotted_view_vertical_1.setBackground(getContext().getDrawable(R.drawable.dotted_line_blue_100_vertical));
            dotted_view_vertical_2.setBackground(getContext().getDrawable(R.drawable.dotted_line_blue_100_vertical));
            dotted_view_vertical_3.setBackground(getContext().getDrawable(R.drawable.dotted_line_blue_100_vertical));
            dotted_view_vertical_4.setBackground(getContext().getDrawable(R.drawable.dotted_line_blue_100_vertical));
            positionCenterLL.setBackground(getContext().getDrawable(R.color.blue_100));
            longitudeTv.setTextColor(getContext().getColor(R.color.white));
            latitudeTv.setTextColor(getContext().getColor(R.color.white));
            altitudeTv.setTextColor(getContext().getColor(R.color.white));
            positionStatus.setTextColor(getContext().getColor(R.color.white));
            speedTv.setTextColor(getContext().getColor(R.color.white));
            directionTv.setTextColor(getContext().getColor(R.color.white));
            satelliteNumber.setTextColor(getContext().getColor(R.color.white));
            hdopTv.setTextColor(getContext().getColor(R.color.white));
           // positionCenterLL.setBackground(getContext().getDrawable(R.color.backgroundMain));
        }
    }

    @Override
    public void onGSVInfo(List<SatelliteVO> satelliteVOS) {

    }
    @Override
    public void onGGAInfo(BDGGAInfo bdggaInfo) {
        if(longitudeTv==null){
            return;
        }
        longitudeTv.setText(BeidouBoxParams.gpsLongitude);
        latitudeTv.setText(BeidouBoxParams.gpsLatitude);
        hdopTv.setText(bdggaInfo.hdop);
        String status="无效";
        if("1".equals(bdggaInfo.statusIndication)){
            status="单点定位";
        }
        if(!bdggaInfo.antennaHeight.equals("")){altitudeTv.setText(StringUtil.roundDouble(Double.parseDouble(bdggaInfo.antennaHeight),3,false)+"");}
        positionStatus.setText(status);
        satelliteNumber.setText(bdggaInfo.visibleSatellite);

    }

    @Override
    public void onRMCInfo(BDRMCInfo bdrmcInfo) {
        if(speedTv==null){
            return;
        }
        if(!bdrmcInfo.groundSpeed.equals("")){
            double speed_dou = Double.parseDouble(bdrmcInfo.groundSpeed)*1.852;
            speedTv.setText(StringUtil.roundDouble(speed_dou,3,false)+"");
        }else {
            speedTv.setText("--");
        }
        if(!bdrmcInfo.groundHeading.equals("")){
            directionTv.setText(bdrmcInfo.groundHeading);
        }else {
            directionTv.setText("--");
        }
    }

    public  String ggaConvertLon(String oldStr,int index){
        String newStr="";
        if(oldStr!=null&&!"".equals(oldStr)) {
            String repStr=oldStr.replace(".","");
            String insertStr=StringUtil.stringInsert(repStr,".",index);
            if(Constant.DEGREE_MINUTES_SECONDS_FORMAT==format){
                newStr=StringUtil.changeToDFM(StringUtil.reservedLoc(Double.parseDouble(insertStr),6));
            }else if(Constant.DEGREE_FORMAT==format){
                newStr=StringUtil.reservedLocStr(Double.parseDouble(insertStr),6);
            }
        }
       return newStr;
    }
}
